<?php

defined('SYSPATH') or die('Access error!');

class Model_News_Attachment extends Model
{

    /**
     *  获取新闻的附件列表
     * @param type $id
     * @return type
     */
    public function get_news_attachment($id)
    {

        $sql = "SELECT a.* " .
                "FROM " . $this->table . " AS na " .
                "LEFT JOIN " . $this->db->get_table('attachment') . " AS a " .
                "ON na.attachment_id=a.id " .
                "WHERE na.news_id=" . $id;
        $temp = $this->db->get_results($sql);
        $data = array();
        if (!empty($temp))
        {
            foreach ($temp as $key => $value)
            {
                $value['url'] = SYSTEM_NEWS_IMAGES_FILE_PATH . '/' . $value['file_path'] . $value['file_realname'];
                $data[] = $value;
            }
        }
        return $data;
    }

    /**
     *  获取新闻的封面
     * @param type $id
     * @return type
     */
    public function get_news_attachment_first($id)
    {

        $sql = "SELECT a.* " .
                "FROM " . $this->table . " AS na " .
                "LEFT JOIN " . $this->db->get_table('attachment') . " AS a " .
                "ON na.attachment_id=a.id " .
                "WHERE na.news_id=" . $id;
        $data = $this->db->get_row($sql);
        if (!empty($data))
        {
            $data['url'] = SYSTEM_NEWS_IMAGES_FILE_PATH . '/' . $data['file_path'] . $data['file_realname'];
        }
        return $data;
    }

}